#include <stdio.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <math.h>
#include <string.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>

#include "eeprom.h"


#define EEPROM_TAG		0x5A
#define EEPROM_VERSION	0x02


stEepromData_t Eeprom;


void EEPROM_init(void)
{
	uint8_t i, v;
	i = eeprom_read_byte( (const void*)0 );
	v = eeprom_read_byte( (const void*)1 );
	if( ( i != EEPROM_TAG ) || ( v != EEPROM_VERSION ) ) {
		//initialise with standard settings
		Eeprom.backlightLevel = 128;
		Eeprom.backlightLevelLightsOn = 192;
		Eeprom.backlightLevelLightsOff = 0xff;
		Eeprom.backlightTime = 20;
		Eeprom.units = 0;

	}
	else {
		eeprom_read_block( (void*)&Eeprom, (const void*)2, sizeof(stEepromData_t) );
	}
}


void EEPROM_write(void)
{
	eeprom_write_byte( (const void*)0, EEPROM_TAG );
	eeprom_write_byte( (const void*)1, EEPROM_VERSION );
	eeprom_write_block( (const void*)&Eeprom, (void*)2, sizeof(stEepromData_t) );
}
